#! /bin/bash

# Settings

MAVEN_VERSION="apache-maven-3.3.9"
   MAVEN_HOME="/data/app/jenkins/tools/$MAVEN_VERSION"
    MAVEN_CMD="$MAVEN_HOME/bin/mvn"
   MAVEN_OPTS="-U clean -Dmaven.test.skip=true package dependency:tree"

MAVEN_POM_EXTRA_ASPECTJ=$CONFIG_DIR/project/pay/pom-extra-aspectj.xml

MAVEN_ARCHIVE_DIR=Archive/$APP_PRJ/$APP_SUB/`date +%Y/%m/%d`
MAVEN_ARCHIVE_EXPIRES=15

MavenActionAdapter(){
    test "$1" = "PPP" && MavenPomPreProcess 
    MavenBuild
}

MavenArchiveController(){
     maven_archive_target=$PKG_FILE
    archive_saved_to_name=$JKS_DL_DIR/$MAVEN_ARCHIVE_DIR/$BUILD_NUMBER-$(basename $maven_archive_target)         
          old_archive_dir=$JKS_DL_DIR/$MAVEN_ARCHIVE_DIR/`date -d "$MAVEN_ARCHIVE_EXPIRES days ago" ++%Y/%m/%d`
                     astd=$(dirname $archive_saved_to_name)
    if [ ! -d $astd ]
    then 
        msg i "Create dir $astd"
        mkdir -p $astd
    fi

    test -f "$maven_archive_target" && cp -rf $maven_archive_target $archive_saved_to_name
            
    msg i "Maven Project Archived to $archive_saved_to_name"
    msg i "Maven Project Archived URL: $JKS_DL_URL_19/$MAVEN_ARCHIVE_DIR/$(basename $archive_saved_to_name)"

    if [ -d "$old_archive_dir" ]
    then
        msg i "Remove $old_archive_dir"
        remove $old_archive_dir
    else
        msg i "Maven archive not found. Skipped"
    fi
}


MavenBuild(){
    msg i "Using $MAVEN_VERSION"
    msg i "With maven options: $MAVEN_OPTS"
    $MAVEN_CMD $MAVEN_OPTS
    
    test $? != 0 && msg error "Failed to build, aborted"     

    msg info "Check file $PKG_FILE"
    check file $PKG_FILE
}

MavenPomPreProcess(){
    
    msg i "Preprocessing file pom.xml"
    sed -i '/jdk.version/s/1.6/1.8/' pom.xml 

    total_line=`wc -l pom.xml | awk '{print $1}'`
     head_line=`grep -n '<dependencies>' pom.xml | awk -F ':' '{print $1}'`
     tail_line=`echo $total_line - $head_line | bc`

    p_head=`mktemp $TEMP_DIR/XXXXXXXXXXXX`
    p_tail=`mktemp $TEMP_DIR/XXXXXXXXXXXX`

    head -$head_line pom.xml > $p_head
    tail -$tail_line pom.xml > $p_tail

    cat $MAVEN_POM_EXTRA_ASPECTJ    >> $p_head
    cat $p_tail                     >> $p_head

    cp -rf $p_head pom.xml

    remove $p_head
    remove $p_tail
}
